/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RoomPanel.GameRoom;

import RoomPanel.RoomPanel;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

/**
 *
 * @author 52194291
 */
public class GameRoomPanel extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
    */
    
    private static HttpSession session;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        session = RoomPanel.getSession();
        //String uid = (String) session.getAttribute("uid");
        String uid = "000003";
        String[][] room = new String[3][2];
        for(int i=0;i<room.length;i++)
            for(int j=0;j<room[i].length;j++)
                room[i][j] = "";
        setRoomInfo(room);
        
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet GameRoomPanel</title>");   
            
            out.println("<style type=\"text/css\">");
            out.println(".button{height:80px;width:30%;}");
            out.println("#room1{background-color:#d0e4fe;width:100%;height:100px}");
            out.println("#room2{background-color:#de04ce;width:100%;height:100px}");
            out.println("#room3{background-color:#a4e6c3;width:100%;height:100px}");
            out.println("h1{background-color:RED;color:WHITE;}");
            out.println("</style>");
            
            //Javascript
            out.println("<script type=\"text/javascript\">");
            out.println("function show_alert(value, id){");
            out.println("   if(value == 'Empty'){");
            out.println("       var response = confirm(\"Would u like to join this game?\");");
            out.println("       if(response == true)");
            out.println("           document.getElementById(id).value="+uid);
            out.println("   }");
            out.println("   else{");
            out.println("       alert(\"Room Taken\");");
            out.println("   }");
            out.println("}");
            out.println("</script>");  
            
            out.println("</head>");
            out.println("<body>");
            
            out.println("<h1> Game Room List </h1>");
            out.println("<br /><br />");
            
            out.println("<div id=\"room1\">");
            out.println(String.format("<input type=\"button\" id=\"player1-rm1\" class=\"button\" value=\"%s\" onclick=\"show_alert(this.value, this.id)\"></button> ", room[0][0]));
            out.println(String.format("<input type=\"button\" id=\"player2-rm1\" class=\"button\" value=\"%s\" onclick=\"show_alert(this.value, this.id)\"></button> ", room[0][1]));
            out.println(String.format("<input type=\"button\" id=\"race-rm1\" class=\"button\" value=\"%s\" onclick=\"show_alert(this.value, this.id)\"></button> ", "Race"));
            out.println("</div>");
            
            out.println("<div id=\"room2\">");
            out.println(String.format("<input type=\"button\" id=\"player1-rm2\" class=\"button\" value=\"%s\" onclick=\"show_alert(this.value, this.id)\"></button> ", room[1][0]));
            out.println(String.format("<input type=\"button\" id=\"player2-rm2\" class=\"button\" value=\"%s\" onclick=\"show_alert(this.value, this.id)\"></button> ", room[1][1]));
            out.println(String.format("<input type=\"button\" id=\"race-rm2\" class=\"button\" value=\"%s\" onclick=\"show_alert(this.value, this.id)\"></button> ", "Race"));
            out.println("</div>");
            out.println("<div id=\"room3\">");
            out.println(String.format("<input type=\"button\" id=\"player1-rm3\" class=\"button\" value=\"%s\" onclick=\"show_alert(this.value, this.id)\"></button> ", room[2][0]));
            out.println(String.format("<input type=\"button\" id=\"player2-rm3\" class=\"button\" value=\"%s\" onclick=\"show_alert(this.value, this.id)\"></button> ", room[2][1]));
            out.println(String.format("<input type=\"button\" id=\"race-rm3\" class=\"button\" value=\"%s\" onclick=\"show_alert(this.value, this.id)\"></button> ", "Race"));
            out.println("</div>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }
    
    private void setRoomInfo(String[][] room){
        ResultSet rs = getAllRoomInfo();
        try {
            int counter = 0;
            while(rs != null && rs.next()){
                if(rs.getInt("roomId") == 0){
                    if(room[0][0].isEmpty())
                        room[0][0] = rs.getString("UID");
                    else
                        room[0][1] = rs.getString("UID"); 
                }else if(rs.getInt("roomId") == 1){
                    if(room[1][0].isEmpty())
                        room[1][0] = rs.getString("UID");
                    else
                        room[1][1] = rs.getString("UID");
                }else if(rs.getInt("roomId") == 2){
                    if(room[2][0].isEmpty())
                        room[2][0] = rs.getString("UID");
                    else
                        room[2][1] = rs.getString("UID");
                }
                counter++;
            }
            if(counter < 6){
                while(counter < 6){
                    if(room[0][0].isEmpty())
                        room[0][0] = "Empty";
                    else if(room[0][1].isEmpty())
                        room[0][1] = "Empty";
                    else if(room[1][0].isEmpty())
                        room[1][0] = "Empty";
                    else if(room[1][1].isEmpty())
                        room[1][1] = "Empty";
                    else if(room[2][0].isEmpty())
                        room[2][0] = "Empty";
                    else if(room[2][1].isEmpty())
                        room[2][1] = "Empty";
                    counter++;
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(GameRoomPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private ResultSet getAllRoomInfo(){
        try {
            Context initCtx = new InitialContext();
                Context envCtx = (Context) initCtx.lookup("java:comp/env");
                DataSource ds = (DataSource) envCtx.lookup("jdbc/FootballManager");
                Connection con = ds.getConnection();
                Statement stmt = con.createStatement();
                String sql = String.format("SELECT * FROM Customers_Room");
                return stmt.executeQuery(sql);
        } catch (SQLException ex) {
            Logger.getLogger(GameRoomPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(GameRoomPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
